package com.yunding.house.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunding.house.pojo.Staff;
import com.yunding.house.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author linyang
 * @since 2019-08-15
 */
/*操作权限赋予，这个操作只有负责人可以进行操作*/
@RestController
@RequestMapping("/staff")
public class StaffController {
    /*分页查找所有*/

    /*删除通过ID*/

    /*增加*/

    /*修改*/
    @Autowired
    private StaffService staffService;


    @RequestMapping("/list")
    @ResponseBody
    public ModelAndView list(Model model, @RequestParam(value = "start", defaultValue = "0") Integer start) {
        start = start < 0 ? 0 : start;
        PageHelper.startPage(start, 5);
        List<Staff> list = staffService.findAll();
        PageInfo<Staff> pagehelper = new PageInfo<>(list);
        System.out.println(pagehelper);
        model.addAttribute("pagehelper", pagehelper);
        model.addAttribute("stafflist", staffService.findAll());
        model.addAttribute("title", "查看员工");
        return new ModelAndView("staff/list", "staffmodel", model);

    }

    @RequestMapping("/selectone")
    @ResponseBody
    public ModelAndView selectone(Model model, int page, int size, Staff staff) {
        model.addAttribute("staff", staffService.search(5, 4, staff));
        return new ModelAndView("staff/view", "staffmodel", model);
    }

    @GetMapping("/form")
    @ResponseBody
    public ModelAndView createForm(Model model) {
        model.addAttribute("staff", new Staff());
        model.addAttribute("title", "云集家员工信息注入");
        return new ModelAndView("staff/register", "staffmodel", model);
    }

    @RequestMapping("/add")
    @ResponseBody
    public ModelAndView add(Staff staff) {
        System.out.println("保存的员工信息" + staff);
        staffService.save(staff);
        return new ModelAndView("redirect:/staff/list");
    }

    @RequestMapping("/modify/{id}")
    @ResponseBody
    public ModelAndView modify(@PathVariable("id") long id, Model model) {
        Staff staff = staffService.selectone(id);
        model.addAttribute("title", "修改员工");
        model.addAttribute("staff", staff);
        return new ModelAndView("staff/form", "staffmodel", model);
    }

    @RequestMapping("/update")
    @ResponseBody
    public ModelAndView update(Staff staff) {
        System.out.println("更改" + staff);
        staffService.update(staff);
        return new ModelAndView("redirect:/staff/list");
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public ModelAndView delete(@PathVariable("id") long id, Model model) {
        staffService.del(id);
        return new ModelAndView("redirect:/staff/list");
    }


}
